
function delay (time) {
  return new Promise((resolve,reject)=>{
    console.log(`wait ${time}s`)
    setTimeout(() => {
      console.log('execute')
      resolve()
    }, time * 1000);
  })
}

let arr = [3,4,5,6]

// 方法 1  reduce

// arr.reduce((pre,cur)=>{
//   return pre.then(()=>delay(cur))
// },Promise.resolve())

// 方法 2 async await
// async function executePromise(arr) {
//   for (let index = 0; index < arr.length; index++) {
//     const e = arr[index]
//     await delay(e)
//   }
// }
// executePromise(arr)


// 方法 3 普通循环+变量赋值

// let p = Promise.resolve()
// for (let index = 0; index < arr.length; index++) {
//   const e = arr[index]
//   p = p.then(()=>delay(e))
// }

// let p = Promise.resolve()
// while(arr.length) {
//   console.log(arr)
//   const e = arr.shift()
//   p = p.then(()=>delay(e))
// }

let i,p = Promise.resolve()
while(i=arr.shift()){
  let s = i
  p = p.then(()=>delay(s))
}
